package com.ddtech.business.check.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ddtech.business.check.entity.CheckUserRecord;
import com.ddtech.business.check.query.CheckQuery;
import com.ddtech.business.check.service.CheckService;
import com.ddtech.business.check.vo.req.AddCheck;
import com.ddtech.business.check.vo.req.CheckRecord;
import com.ddtech.business.check.vo.res.QueTypeRes;
import com.ddtech.business.check.vo.res.ResUserCheckDetail;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 考核表 前端控制器
 * </p>
 *
 * @author bin_pu
 * @since 2023-02-06
 */
@Api(tags = "业务-考试管理")
@RestController
@RequestMapping("/check")
public class CheckController {
    @Autowired
    private CheckService checkService;

    @ApiOperation(value = "查询考核列表  ")
    @GetMapping(value = "/page-list")
    public ResponseEntity getPageList(CheckQuery checkQuery) {
        return new ResponseEntity(checkService.pageList(checkQuery), HttpStatus.OK);
    }

    @ApiOperation(value = "创建考核信息")
    @PostMapping(value = "/create")
    public ResponseEntity create(@RequestBody AddCheck addCheck) {
        checkService.create(addCheck);
        return new ResponseEntity(HttpStatus.OK);
    }

    @ApiOperation(value = "查询考核详情")
    @GetMapping(value = "/info/{id}")
    public ResponseEntity info(@PathVariable String id) {
        return new ResponseEntity(checkService.getInfo(id), HttpStatus.OK);
    }


    @ApiOperation(value = "删除考核")
    @PostMapping(value = "/del/{id}")
    public ResponseEntity del(@PathVariable String id) {
        checkService.del(id);
        return new ResponseEntity(HttpStatus.OK);
    }

    @ApiOperation(value = "查询用户考试列表")
    @GetMapping(value = "/user/list")
    public ResponseEntity getUserList(@ApiParam("类型:1考试,2竞赛,3、已完成列表") @RequestParam Integer type,
                                      @ApiParam("日期") String date) {
        return new ResponseEntity(checkService.getCheckList(type, date), HttpStatus.OK);
    }

    @ApiOperation(value = "查询考试记录")
    @GetMapping(value = "/user/exam")
    public ResponseEntity getUserExam( @RequestParam @ApiParam("页码") int page,
                                       @RequestParam @ApiParam("页面大小") int size,
                                       @ApiParam("类型:1考试,2竞赛,3、已完成列表") @RequestParam Integer type,
                                        String inputSearch) {
        return new ResponseEntity(checkService.getUserExam(page,size,type,inputSearch), HttpStatus.OK);
    }


    @ApiOperation(value = "考试")
    @GetMapping("/exam")
    public ResponseEntity<QueTypeRes> exam(@RequestParam @ApiParam("试卷id") String checkId) {
        return new ResponseEntity(checkService.exam(checkId), HttpStatus.OK);
    }


    @ApiOperation(value = "提交答卷")
    @PostMapping("/commit")
    public ResponseEntity commit(@RequestBody @ApiParam("答题记录实体") CheckRecord checkRecord) {
        CheckUserRecord checkUserRecord = checkService.commit(checkRecord);
        return new ResponseEntity(checkUserRecord, HttpStatus.OK);
    }


    @ApiOperation(value = "获取用户答题详情")
    @GetMapping("/getUserExamDetail/{recordId}")
    public ResponseEntity<ResUserCheckDetail> getUserExamDetail(@PathVariable("recordId") @ApiParam("答题情况id") String recordId) {
        return new ResponseEntity(checkService.getUserExamDetail(recordId), HttpStatus.OK);
    }


    @ApiOperation(value = "获取排名")
    @GetMapping("/getRank")
    public ResponseEntity getRank(@RequestParam @ApiParam("考核id") String checkId,
                                  @RequestParam @ApiParam("页码") int page,
                                  @RequestParam @ApiParam("页面大小") int size) {
        IPage pageableRequest = new Page(page+1, size);
        return new ResponseEntity(checkService.getRank(checkId,null, pageableRequest), HttpStatus.OK);
    }


    @ApiOperation(value = "查询考试记录")
    @GetMapping(value = "/user/exam-statistics")
    public ResponseEntity getUserExamStatistics(@ApiParam("类型:1考试,2竞赛") @RequestParam Integer type) {
        return new ResponseEntity(checkService.getUserExamStatistics(type), HttpStatus.OK);
    }
}
